﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfMongoDB.Model;

namespace WpfMongoDB
{
    /// <summary>
    /// ConnectionForm.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectionForm : Window
    {
        public string Name;
        public ConnectionForm()
        {
            InitializeComponent();
        }

        private void btnok_Click(object sender, RoutedEventArgs e)
        {
            var uname = txtuname.Text;
            var pwd = txtpwd.Password;
            var dataBase = string.IsNullOrEmpty(txtDatabase.Text) ? "admin" : txtDatabase.Text;
            MongoClientSettings mcs = new MongoClientSettings();
            mcs.Server = new MongoServerAddress(txthost.Text, Convert.ToInt32(txtport.Text));//
            MongoClient c = new MongoClient(mcs);
            var server = c.GetServer();
            var key=string.Format("{0}:{1}", txthost.Text, txtport.Text);
            if (!StaticCs.Servers.ContainsKey(key))
	        {
                StaticCs.Servers.Add(key,server);
                Name = key;
	        }
            this.Close();
        }

        private void btnexit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 测试链接地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            var uname = txtuname.Text;
            var pwd = txtpwd.Password;
            var dataBase = string.IsNullOrEmpty(txtDatabase.Text) ? "local" : txtDatabase.Text;
            MongoClientSettings mcs = new MongoClientSettings();
            mcs.Server = new MongoServerAddress(txthost.Text, Convert.ToInt32(txtport.Text));//
            MongoClient c = new MongoClient(mcs);
            var server = c.GetServer();
            bool istrue = false;
            
            try
            {
                if (!string.IsNullOrEmpty(uname) && !string.IsNullOrEmpty(pwd))
                {
                    MongoCredentials credentials = new MongoCredentials(uname, pwd);
                    istrue = server.DatabaseExists(dataBase, credentials);
                }
                else
                {
                    istrue = server.DatabaseExists(dataBase);
                }
                if (istrue)
                {
                    MessageBox.Show("链接测试成功！");
                }
                else
                {
                    MessageBox.Show("链接测试失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
